package vn.student;

import vn.student.controller.ViewEditCourseDetailEditorController;
import vn.student.mylib.DateDateDialog;
import vn.student.mylib.TextInputDialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;

	/*
	
		Class name: ViewCourseDetailEditor.java
		Function: Day la lop ke thua lop Activity, la lop khoi tao cua so Activity 
			dung de xem thong tin chi tiet mot mon hoc va cho phep chinh sua thong tin chi tiet.
		
	*/

public class ViewCourseDetailEditor extends Activity{
	String id = null;
	Activity act = this;
	ViewEditCourseDetailEditorController controller = null;
	boolean isCreateNew = false;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_course_detail);
		RelativeLayout tmp = (RelativeLayout) findViewById(R.id.button01);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickBackButton();
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.button02);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickSaveButton();
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.editNameBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editCourseName();
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.editDurationBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editDuration();
			}
		});
		Intent intent = this.getIntent();
		id = intent.getExtras().getString("id");
		isCreateNew = intent.getExtras().getBoolean("isCreateNew");
		if(isCreateNew)
		{
			String semesterID = intent.getExtras().getString("semesterID");
			controller = new ViewEditCourseDetailEditorController(this,id,isCreateNew,semesterID);
		}
		else
			controller = new ViewEditCourseDetailEditorController(this,id,isCreateNew);
		controller.showCourseDetail(id);
	}
	private void onClickBackButton(){
		this.setResult(Activity.RESULT_CANCELED);
		this.finish();
	}
	private void onClickSaveButton(){
		if(controller.updateCourseInfo()){
			this.setResult(Activity.RESULT_OK);
			this.finish();
		}
		else{
			//Create a builder 
		      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
		      builder.setTitle("Alert Window"); 
		       
		      //Create the dialog 
		      AlertDialog ad = builder.create(); 
		       
		      //show 
		      ad.show(); 
		}
			
	}
	private void editDuration(){
		String[] dateStart = controller.courseInfo.dateStart.split("-");
		if(dateStart.length!=3)
			return;
		String[] dateEnd = controller.courseInfo.dateEnd.split("-");
		if(dateEnd.length!=3)
			return;
		DateDateDialog.viewDialog("Course duration",dateStart,dateEnd, act, new DateDateDialogOnOkClickListener());
	}
	private void editCourseName(){
		TextInputDialog.viewDialog("Input course name",controller.courseInfo.courseName, act, new InputTextDialogOnOkClickListener());
	}
	class DateDateDialogOnOkClickListener implements android.content.DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			String tmp;
			DatePicker date = (DatePicker) ((AlertDialog)dialog).findViewById(R.id.dateStartPicker);
			String dateStartStr = "";
			dateStartStr = String.valueOf(date.getYear()) + "-";
			tmp = String.valueOf(date.getMonth()+1);
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateStartStr += tmp + "-";
			tmp = String.valueOf(date.getDayOfMonth());
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateStartStr += tmp;
			
			date = (DatePicker) ((AlertDialog)dialog).findViewById(R.id.dateEndPicker);
			
			String dateEndStr = "";
			dateEndStr = String.valueOf(date.getYear()) + "-";
			tmp = String.valueOf(date.getMonth()+1);
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateEndStr += tmp + "-";
			tmp = String.valueOf(date.getDayOfMonth());
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateEndStr += tmp;
			
			controller.editDuration(dateStartStr,dateEndStr);
			
			controller.showCourseDetail();
		}
	}
	class InputTextDialogOnOkClickListener implements android.content.DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			String name = ((EditText) ((Dialog) dialog).findViewById(R.id.editTextView)).getText().toString();
			controller.editCourseName(name);

			controller.showCourseDetail();
		}
	}
}
